#pragma once
#include <functional>
#include <iostream>
#include <string>
#include <map>

void serviceIO(int sockfd)
{
    char buffer[1024];
    while (true)
    {
        ssize_t n = read(sockfd, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "recv message : " << buffer << std::endl;
            std::string outbuffer(buffer);
            outbuffer += "server [echo]";
            write(sockfd, outbuffer.c_str(), outbuffer.size());
        }
        else if (n == 0)
        {
            logMessage(NORMAL, "client exit");
            break;
        }
    }
    close(sockfd);
}

class Task
{
    using func_t = std::function<void(int)>;

public:
    Task()
    {
    }

    Task(int sockfd, func_t func)
        : _sockfd(sockfd), __call_back__(func)
    {
    }

    void operator()()
    {
        __call_back__(_sockfd);
    }

private:
    int _sockfd;
    func_t __call_back__;
};
